package com.yangguang.huahui.mvp.presenter;

import com.yangguang.huahui.entity.PublishList;
import com.yangguang.huahui.mvp.model.PurchaseModel;
import com.yangguang.huahui.mvp.model.SupplyModel;
import com.yangguang.huahui.mvp.view.PublishListView;
import com.yangguang.huahui.rx.RxManager;
import com.yangguang.huahui.rx.RxSubscriber;

import rx.Observable;

/**
 * Created by Administrator on 2017/3/29.
 */

public class PublishListPresent extends BasePresenter<PublishListView>{
    private int type;
    public PublishListPresent(int type) {
        this.type = type;
    }
    public void getPublish(String uid){
        Observable<PublishList> observable = null;
        switch (type){
            case 1:
                observable = new PurchaseModel().getPublish(uid);
                break;
            case 2:
                observable = new SupplyModel().getPublish(uid);
                break;
        }
        mSubscription = RxManager.getInstance().doSubscribe(observable, new RxSubscriber<PublishList>(false) {
            @Override
            protected void _onNext(PublishList publishList) {
                mView.onSuccess(publishList);
            }

            @Override
            protected void _onError() {
                mView.onError();
            }
        });
    }

    public void delete(String id){
        Observable<PublishList> observable = null;
        switch (type){
            case 1:
                observable = new PurchaseModel().delete(id);
                break;
            case 2:
                observable = new SupplyModel().delete(id);
                break;
        }
        mSubscription = RxManager.getInstance().doSubscribe(observable, new RxSubscriber<PublishList>(false) {
            @Override
            protected void _onNext(PublishList publishList) {
                mView.onSuccess(publishList);
            }

            @Override
            protected void _onError() {
                mView.onError();
            }
        });
    }

    public void refresh(String id, String uid){
        Observable<PublishList> observable = null;
        switch (type){
            case 1:
                observable = new PurchaseModel().refresh(id, uid);
                break;
            case 2:
                observable = new SupplyModel().refresh(id, uid);
                break;
        }
        mSubscription = RxManager.getInstance().doSubscribe(observable, new RxSubscriber<PublishList>(false) {
            @Override
            protected void _onNext(PublishList publishList) {
                mView.onSuccess(publishList);
            }

            @Override
            protected void _onError() {
                mView.onError();
            }
        });
    }

}
